package vn.anhqn.englishsentensetranslator.onlinetranslate;

import main.java.com.memetix.mst.language.Language;
import vn.anhqn.englishsentensetranslator.R;
import vn.anhqn.englishsentensetranslator.addsentense.AddSentenseFragment;
import vn.anhqn.englishsentensetranslator.base.fragment.FragmentHaveSlidingMenu;
import vn.anhqn.englishsentensetranslator.constant.ConfigApp;
import vn.anhqn.englishsentensetranslator.constant.Constant;
import vn.anhqn.englishsentensetranslator.model.TranslateModel;
import vn.anhqn.englishsentensetranslator.utils.AudioUtils;
import vn.anhqn.englishsentensetranslator.utils.AudioUtils.IPlaybackListener;
import vn.anhqn.englishsentensetranslator.utils.PreferenceHelper;
import vn.anhqn.englishsentensetranslator.utils.Utils;
import vn.anhqn.englishsentensetranslator.widget.HeverticalTextView;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.ExtractedText;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class OnlineTranslateFragment extends FragmentHaveSlidingMenu implements IPlaybackListener {
	HeverticalTextView tvFromLang;
	ImageButton btnSwap;
	HeverticalTextView tvToLang;
	ImageView imvFromRemoveAll;
	ImageView imvFromCopy;
	ImageView imvFromSend;
	ImageView imvFromSound;
	ImageView imvToRemoveAll;
	ImageView imvToCopy;
	ImageView imvToSend;
	ImageView imvToSound;
	Button btnTranslate;
	Button btnSave;
	int idPlaying;

	TextView tvResultLang;
	EditText editFromLang;
	boolean isTranslating = false;
	private TranslateModel transModel;
	private Language fromLang = Language.ENGLISH;
	private Language toLang = Language.ENGLISH;
	String[] arrLanguages;
	private int indexLang = 0;
	String extraText = null;

	@Override
	public int getResLayout() {
		return R.layout.online_translate_fragment;
	}

	@Override
	public void setBerhaviorView(View mView) {
		indexLang = PreferenceHelper.getInstance(getActivity()).getInt(ConfigApp.PREFERENCES_LANGUAGE_INDEX, 0);
		arrLanguages = getActivity().getResources().getStringArray(R.array.language_arr);
		toLang = Language.fromIndex(indexLang);
		tvFromLang = (HeverticalTextView) mView.findViewById(R.id.tv_from_lang);
		tvFromLang.setText(R.string.english);

		btnSwap = (ImageButton) mView.findViewById(R.id.btn_swap_lang);

		btnSwap.setOnClickListener(this);

		tvToLang = (HeverticalTextView) mView.findViewById(R.id.tv_to_lang);
		tvToLang.setText(arrLanguages[indexLang]);

		editFromLang = (EditText) mView.findViewById(R.id.edit_from_lang);
		Utils.getIntance().setFontFromAsset(editFromLang, "Helvetica/helvetica-normal.ttf");
		if (getExtraText() != null)
			editFromLang.setText(getExtraText());
		imvFromRemoveAll = (ImageView) mView.findViewById(R.id.imv_fromlang_delete);
		imvFromRemoveAll.setOnClickListener(this);

		imvFromCopy = (ImageView) mView.findViewById(R.id.imv_fromlang_copy);
		imvFromCopy.setOnClickListener(this);

		imvFromSend = (ImageView) mView.findViewById(R.id.imv_fromlang_send);
		imvFromSend.setOnClickListener(this);

		imvToCopy = (ImageView) mView.findViewById(R.id.imv_tolang_copy);
		imvToCopy.setOnClickListener(this);

		imvToSend = (ImageView) mView.findViewById(R.id.imv_tolang_send);
		imvToSend.setOnClickListener(this);

		imvFromSound = (ImageView) mView.findViewById(R.id.imv_fromlang_sound);
		imvFromSound.setOnClickListener(this);

		imvToSound = (ImageView) mView.findViewById(R.id.imv_tolang_sound);
		imvToSound.setOnClickListener(this);

		btnTranslate = (Button) mView.findViewById(R.id.btn_translate);
		btnTranslate.setOnClickListener(this);
		Utils.getIntance().setFontFromAsset(btnTranslate, "Helvetica/helvetica-normal.ttf");

		btnSave = (Button) mView.findViewById(R.id.btn_save);
		btnSave.setOnClickListener(this);
		Utils.getIntance().setFontFromAsset(btnSave, "Helvetica/helvetica-normal.ttf");
		tvResultLang = (TextView) mView.findViewById(R.id.tv_result_lang);
		Utils.getIntance().setFontFromAsset(tvResultLang, "Helvetica/helvetica-normal.ttf");
		tvResultLang.setMovementMethod(new ScrollingMovementMethod());

		if (PreferenceHelper.getInstance(getActivity()).getInt(Constant.KEY_EXTRA_TRANS_TYPE, Constant.E_TO_Y_SENTENSE) == Constant.Y_TO_E_SENTENSE) {
			swapLang();
		}
		setVisibleSpeak();
	}

	private String getExtraText() {
		Bundle bundle = getActivity().getIntent().getExtras();
		if (bundle != null && extraText == null) {
			extraText = bundle.getString(Intent.EXTRA_TEXT);
			getActivity().getIntent().removeExtra(Intent.EXTRA_TEXT);
		}
		return extraText;
	}

	private void setVisibleSpeak() {
		if (fromLang == Language.ENGLISH) {
			imvFromSound.setVisibility(View.VISIBLE);
			imvToSound.setVisibility(View.GONE);
		} else {
			imvFromSound.setVisibility(View.GONE);
			imvToSound.setVisibility(View.VISIBLE);
		}
	}

	public void translateExcute() {
		new TextTranslateTask(fromLang, toLang) {
			protected void onPreExecute() {
				isTranslating = true;
				tvResultLang.setText(R.string.online_tranlsate_translating);
			};

			protected void onPostExecute(TranslateModel result) {
				transModel = result;
				if (transModel != null) {
					tvResultLang.setText(transModel.getResult());
				} else {
					Toast.makeText(getActivity(), R.string.toast_check_network, Toast.LENGTH_SHORT).show();
					tvResultLang.setText("");
				}
				isTranslating = false;
			};
		}.execute(editFromLang.getText().toString());
	}

	private void swapLang() {
		Language tempLang = fromLang;
		fromLang = toLang;
		toLang = tempLang;
		String tempText = tvFromLang.getText().toString();
		tvFromLang.setText(tvToLang.getText().toString());
		tvToLang.setText(tempText);
		setVisibleSpeak();

	}

	@Override
	public void onClickView(View v) {
		int idView = v.getId();
		switch (idView) {

		case R.id.btn_swap_lang:
			swapLang();
			int transType = PreferenceHelper.getInstance(getActivity())
					.getInt(Constant.KEY_EXTRA_TRANS_TYPE, Constant.E_TO_Y_SENTENSE);
			if (transType == Constant.E_TO_Y_SENTENSE)
				PreferenceHelper.getInstance(getActivity()).putInt(Constant.KEY_EXTRA_TRANS_TYPE, Constant.Y_TO_E_SENTENSE);
			else if (transType == Constant.Y_TO_E_SENTENSE) {
				PreferenceHelper.getInstance(getActivity()).putInt(Constant.KEY_EXTRA_TRANS_TYPE, Constant.E_TO_Y_SENTENSE);
			}

			if (!isTranslating && !editFromLang.getText().toString().isEmpty()) {
				editFromLang.setText(tvResultLang.getText().toString());
				translateExcute();
			}
			break;
		case R.id.btn_translate:
			translateExcute();
			break;
		case R.id.imv_fromlang_delete:
			editFromLang.setText("");
			break;
		case R.id.imv_fromlang_copy:
			if (editFromLang.getText() != null)
				Utils.getIntance().copyTextToClipboad(getActivity(), editFromLang.getText().toString(), R.string.copy_messege);
			break;
		case R.id.imv_fromlang_send:
			if (editFromLang.getText() != null)
				Utils.getIntance().startActivityChooser(getActivity(), editFromLang.getText().toString());
			break;
		case R.id.imv_tolang_send:
			if (tvResultLang.getText() != null)
				Utils.getIntance().startActivityChooser(getActivity(), tvResultLang.getText().toString());
			break;
		case R.id.imv_tolang_copy:
			if (tvResultLang.getText() != null)
				Utils.getIntance().copyTextToClipboad(getActivity(), tvResultLang.getText().toString(), R.string.copy_messege);
			break;

		case R.id.imv_tolang_sound:
			if (transModel != null) {
				if (idPlaying != R.id.imv_tolang_sound) {
					AudioUtils.getIntance().playAudioFromUrl(transModel.getSoundTo());
					AudioUtils.getIntance().setPlayListener(this);
					idPlaying = R.id.imv_tolang_sound;
					imvToSound.setImageResource(R.drawable.ic_action_pause);
				} else {
					AudioUtils.getIntance().stopPlay();
					imvToSound.setImageResource(R.drawable.ic_action_headphones);
					idPlaying = 0;
				}

			}
			break;
		case R.id.imv_fromlang_sound:
			if (transModel != null) {
				if (idPlaying != R.id.imv_fromlang_sound) {
					AudioUtils.getIntance().playAudioFromUrl(transModel.getSoundFrom());
					AudioUtils.getIntance().setPlayListener(this);
					idPlaying = R.id.imv_fromlang_sound;
					imvFromSound.setImageResource(R.drawable.ic_action_pause);
				} else {
					AudioUtils.getIntance().stopPlay();
					imvFromSound.setImageResource(R.drawable.ic_action_headphones);
					idPlaying = 0;
				}
			}
			break;
		case R.id.btn_save:
			Bundle bundle = new Bundle();
			bundle.putBoolean(Constant.KEY_EXTRA_IS_FINISH, true);
			String textFrom = "";
			String textTo = "";
			if (fromLang == Language.ENGLISH) {
				if (editFromLang.getText() != null)
					textFrom = editFromLang.getText().toString();
				if (tvResultLang.getText() != null)
					textTo = tvResultLang.getText().toString();
			} else {
				if (editFromLang.getText() != null)
					textTo = editFromLang.getText().toString();
				if (tvResultLang.getText() != null)
					textFrom = tvResultLang.getText().toString();
			}
			bundle.putString(Constant.KEY_EXTRA_FROM_TEXT, textFrom);
			bundle.putString(Constant.KEY_EXTRA_TO_TEXT, textTo);
			replaceNewInstance(AddSentenseFragment.class, bundle);
			break;
		default:
			break;

		}
	}

	@Override
	public void onPre() {
		/*
		 * if(idPlaying ==R.id.imv_tolang_sound) {
		 * imvToSound.setImageResource(R.drawable.ic_action_pause); } else if
		 * (idPlaying ==R.id.imv_fromlang_sound) {
		 * imvFromSound.setImageResource(R.drawable.ic_action_pause); }
		 */
	}

	@Override
	public void onComplete() {

		if (idPlaying == R.id.imv_tolang_sound) {
			imvToSound.setImageResource(R.drawable.ic_action_headphones);
		} else if (idPlaying == R.id.imv_fromlang_sound) {
			imvFromSound.setImageResource(R.drawable.ic_action_headphones);
		}
		idPlaying = 0;
	}

	@Override
	public void onError() {

		if (idPlaying == R.id.imv_tolang_sound) {
			imvToSound.setImageResource(R.drawable.ic_action_headphones);
		} else if (idPlaying == R.id.imv_fromlang_sound) {
			imvFromSound.setImageResource(R.drawable.ic_action_headphones);
		}
		idPlaying = 0;
	}

	@Override
	public void onStop() {
		// Log.w("fragment life", "OnlinetranslateFragment: onstop");
		super.onStop();
	}

	@Override
	public void onDestroy() {
		// Log.w("fragment life", "OnlinetranslateFragment: ondestroy");
		super.onDestroy();
	}

	@Override
	public int getidTitle() {
		return R.string.online_tranlsate_title;
	}
}